package com.lf.plugin.lf125_Plugin;

import android.util.Log;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;

/**
 * This class echoes a string called from JavaScript.
 */
public class LF125Plugin extends CordovaPlugin {

    private LFTagReadHelper mLFTagReaderManager;
    private final String TAG = "LF125Plugin";

    @Override
    public void initialize(org.apache.cordova.CordovaInterface cordova, org.apache.cordova.CordovaWebView webView) {
        super.initialize(cordova, webView);
        mLFTagReaderManager = new LFTagReadHelper();
        mLFTagReaderManager.setContext(cordova.getActivity().getApplicationContext());
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Log.d(TAG,"execute action:"+action);
        if ("init".equals(action)) {
            initPlugin(callbackContext);
            return true;
        } else if ("startRead".equals(action)) {
            startRead(callbackContext);
            return true;
        } else if ("stopRead".equals(action)) {
            stopRead(callbackContext);
            return true;
        } else if("close".equals(action)){
            closeRead(callbackContext);
            return true;
        }else {
            return false;
        }
    }

    private void initPlugin(CallbackContext callbackContext) {
        if(mLFTagReaderManager!=null){
            mLFTagReaderManager.initRead();
            callbackContext.success("Read initialized");
        }
    }

    private void startRead(CallbackContext callbackContext) {
        if(mLFTagReaderManager!=null){
            // 开始读取标签
            mLFTagReaderManager.setMessageCallback((id, country) -> {
                String msg = "id = " + id + "; country = " + country;
                Log.d(TAG, "setMessageCallback msg:" + msg);
                PluginResult result = new PluginResult(PluginResult.Status.OK, msg);
                result.setKeepCallback(true); // 确保回调保持有效
                callbackContext.sendPluginResult(result);
            });
            mLFTagReaderManager.startRead();
            // 立即返回一个空的结果以保持回调有效
            PluginResult result = new PluginResult(PluginResult.Status.OK,"Reading start");
            result.setKeepCallback(true); // 确保回调保持有效
            callbackContext.sendPluginResult(result);
        }
    }

    private void stopRead(CallbackContext callbackContext) {
        if (mLFTagReaderManager != null) {
            // 停止读取标签
            mLFTagReaderManager.stopRead();
            callbackContext.success("Reading stop");
        }
    }

    private void closeRead(CallbackContext callbackContext) {
        if (mLFTagReaderManager != null) {
            mLFTagReaderManager.close();
            callbackContext.success("Reading close");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLFTagReaderManager != null) {
            mLFTagReaderManager.close();
        }
    }
}